//
//  XMLHomeCell.swift
//  Weibo
//
//  Created by mac on 16/5/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
    //原创视图
    //转发视图
    //底部的ToolBar
let margin :CGFloat = 10

class XMLHomeCell: UITableViewCell {
    
    var toolBarTop :Constraint?
    
    var homeCellViewModel :XMLHomeCellViewModel? {
    
        didSet {
        
            originalView.homeCellViewModel = homeCellViewModel
            toolBarTop?.uninstall()
            if homeCellViewModel?.home?.retweeted_status != nil {
                retweetView.homeCellViewModel = homeCellViewModel
                retweetView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else {
                retweetView.hidden = true
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            
            toolBarView.homeCellViewModel = homeCellViewModel
            
//            print(homeCellViewModel?.home?.retweeted_status?.text)
        }
    }

    let originalView :XMLHomeOriginalView = XMLHomeOriginalView()
    let retweetView :XMLHomeRetweetView = XMLHomeRetweetView()
    let toolBarView :XMLHomeToolBarView = XMLHomeToolBarView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addSubViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubViews() {
    
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(40)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
}
